﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using System.Collections.Generic;

public partial class Wedding_ucControl_ucOrder : System.Web.UI.UserControl
{
    TblTypeDAO typeDAO = new TblTypeDAO(Global.CONNECTION_STRING);
    TblCustomersDAO customerDAO = new TblCustomersDAO(Global.CONNECTION_STRING);
    TblMenuDAO menuDAO = new TblMenuDAO(Global.CONNECTION_STRING);

    TblBookingDAO bookingDAO = new TblBookingDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //loadType();
            loadMenu();
            txtPrice.Enabled = false;
          
        } 
       
    }
    private void loadMenu()
    {
        drMenu.DataSource = menuDAO.GetMenu_Wedding(2);
        drMenu.DataTextField = "MenuName";
        drMenu.DataValueField = "IDMenu";
        drMenu.DataBind();
        ListItem lstItem = new ListItem(" ---- Option Menu ---- ", "");
        drMenu.Items.Insert(0, lstItem);
    }

    private bool UserAccount_CheckExists()
    {
        bool bExists = false;
        try
        {
            bExists = customerDAO.CheckExits(txt_email.Text, 1);

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Wedding/Error.aspx", true);
        }
        return bExists;
    }

    // kiem tra khong trung ngay - gio cho Wedding

    private bool CheckDateTime()
    {
        bool bExists = false;
        try
        {
            bExists = bookingDAO.CheckDatetime(Global.ConvertToDate(txt_day.Text), drTime.SelectedItem.Text);

        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect("~/Wedding/Error.aspx", true);
        }
        return bExists;
    }

    protected void bt_set_Click(object sender, EventArgs e)
    {
        bool flag = false;
        if (UserAccount_CheckExists())
        {
            //lblMessage.Text = "Email Exist.";
            ScriptManager.RegisterStartupScript(bt_set, bt_set.GetType(), "alert", "alert('Email Exist!');", true);
            return;
        }
        if (CheckDateTime())
        {
            //lblMessage.Text = "Date Time Exist";
            ScriptManager.RegisterStartupScript(bt_set, bt_set.GetType(), "alert", "alert('DateEvent - TimeEvent Exist !');", true);
            return;
        }
        try
        {
                #region Insert Customer
                // insert Customer
                TblCustomers customer = new TblCustomers();
                customer.FName = txt_fistname.Text;
                customer.LName = txt_lastname.Text;
                customer.Company = "";
                customer.CusAddress = txt_address.Text;
                customer.EmailAdress = txt_email.Text;
                customer.CusState = "";
                customer.Postcode = "";
                customer.Suburb = "";
                customer.HomePhone = "";
                customer.WorkPhone = "";
                customer.MobilePhone = txtmobilephone.Text;
                customer.Password = "";

                String id = Request.QueryString["id"];

                DataTable dt = null;

                if (id == "" || id == null)
                {
                    customer.CusID = 0;
                    dt = customerDAO.Insert_Up(customer, 1);

                    foreach (DataRow dr in dt.Rows)
                    {
                        customer.CusID = Convert.ToInt32(dr["CusID"].ToString());

                    }
                }
                else
                {
                    customer.CusID = Global.ConvertToInt(id);
                    customerDAO.Insert_Up(customer, 2);
                }

                #endregion


                #region Insert Booking

                TblBooking booking = new TblBooking();
                booking.CusID = customer.CusID;
                booking.DateBook = DateTime.Now;
                booking.DateEvent = Convert.ToDateTime(txt_day.Text);
                booking.IDType = 2;
                booking.Meal = drMeal.SelectedItem.Text;
                booking.Notes = txtNode.Text;
                booking.NumberPeople = Convert.ToInt32(cbNumberPoeple.SelectedItem.Text);
                booking.Status = 1;
                booking.TimeEvent = drTime.SelectedItem.Text;
                booking.TotalPrice = Convert.ToDecimal(txtPrice.Text);

                booking.BookID = 0;
                dt = bookingDAO.Insert_UpBooking(booking);

                foreach (DataRow dr2 in dt.Rows)
                {
                    booking.BookID = Convert.ToInt32(dr2["BookID"].ToString());

                }

                #endregion

                #region insert BookingDetail

                TblBookingDetail bookDetail = new TblBookingDetail();
                bookDetail.BookID = booking.BookID;
                bookDetail.Price = Convert.ToDecimal(txtPrice.Text);
                bookDetail.TotalAmount = Convert.ToDecimal(txtPrice.Text);
                bookDetail.IDMenu = Convert.ToInt32(drMenu.SelectedValue);
                bookDetail.Quantity = 1;

                bookDetail.IDBookDetail = 0;
                bookingDAO.Insert_UpBookingDetail(bookDetail);

                #endregion

                flag = true;
            }
       
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE);
        }
        if (flag)
        {
            Response.Redirect("~/Wedding/Default.aspx");
        }
    }


    protected void drMenu_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt =null;
        dt = menuDAO.GetMenuByID(Global.ConvertToInt(drMenu.SelectedValue));

        foreach (DataRow dr in dt.Rows)
        {
            txtPrice.Text = dr["Price"].ToString();
        }
    }
    protected void bt_reset_Click(object sender, EventArgs e)
    {
        txtPrice.Text = String.Empty;
        txt_address.Text = String.Empty;
        txt_day.Text = String.Empty;
        txt_email.Text = String.Empty;
        txt_fistname.Text = String.Empty;
        txt_lastname.Text = String.Empty;
        txtmobilephone.Text = String.Empty;
        txtNode.Text = String.Empty;

       
        
    }
}
